package com.bw.ae.nexus.service;

import com.bw.ae.nexus.Exceptions.*;
import com.bw.ae.nexus.domain.*;
import com.bw.ae.nexus.utils.HttpUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * Created by Xiaosiyuan on 2016/8/23.
 */
@Service
public class NexusFunctionImpl implements NexusFunction {

    @Autowired
    private HttpUtil httpUtil;

    @Override
    public  void createRepo(String projectLabel) throws RepositoryCreateException {
        String jsonStr= JSonString.createRepo.replace("?",projectLabel);
        try {
            httpUtil.request("repository",jsonStr);
        } catch (IOException e) {
            throw new RepositoryCreateException("创建repository失败，可能的原因：该repository已存在、服务器故障。");
        }
    }

    @Override
    public void addRoles(String projectLabel) throws RoleAddException {

        String jsonStr= JSonString.addRoles.replace("?",projectLabel);
        try {
            httpUtil.request("role",jsonStr);
        } catch (IOException e) {
            throw new RoleAddException("创建roles失败，可能的原因：该role已存在、服务器故障。");
        }
    }

    @Override
    public void addUser(User user) throws UserAddException {

        String jsonStr=JSonString.addUser.replace("userId",user.getUserId()).
                replace("firstName",user.getFirstName().equals("")?user.getUserId():user.getFirstName()).
                replace("lastName",user.getLastName().equals("")?user.getUserId():user.getLastName())
                .replace("email",user.getEmail()).replace("password", user.getPassword());
        System.out.println(jsonStr);
        try {
            httpUtil.request("user",jsonStr);
        } catch (IOException e) {
            throw new UserAddException("创建用户失败，可能的原因：信息不完整或错误、服务器故障。");
        }
    }

    @Override
    public void updateRepo(String projectLabel) throws RepositoryUpdateException {
        String jsonStr= JSonString.updateRepo.replace("?",projectLabel);
        try {
            httpUtil.request("updateRepository",jsonStr);
        } catch (IOException e) {
            throw new RepositoryUpdateException("更新ae-public失败，可能的原因：已存在、服务器故障。");
        }
    }

    @Override
    public void updateUser(User user, String projectLabel) throws UserGrantException {
        String jsonStr=JSonString.updateUser.replace("?",projectLabel).replace("userId", user.getUserId());
        try {
            httpUtil.request("updateUser",jsonStr);
        } catch (IOException e) {
            throw new UserGrantException("授权失败，可能的原因：不存在该项目仓库、服务器故障");
        }

    }

    @Override
    public void updateOrAddUser(User user, String projectLabel) throws Exception{
        String jsonStr= JSonString.updateOrAddUser.replace("?",projectLabel).replace("userId", user.getUserId()).replace("firstName",user.getFirstName()).
                replace("lastName",user.getLastName()).replace("email",user.getEmail()).replace("password", user.getPassword());
        httpUtil.request("updateOrAddUser", jsonStr);
    }

    @Override
    public void changePassword(String userId, String oldPassword, String newPassword) throws PasswordChangeException {
        String jsonStr=JSonString.changePassword.replace("userId",userId).replace("oldPassword",oldPassword).replace("newPassword",newPassword);
        try {
            httpUtil.request("changePassword",jsonStr);
        } catch (IOException e) {
            throw new PasswordChangeException("修改密码失败，可能的原因：不存在该用户、原密码错误、服务器故障。");
        }
    }

    @Override
    public void changePassword(String userId, String password) throws PasswordChangeException {
        String jsonStr=JSonString.changePasswordForce.replace("userId",userId).replace("password",password);
        try {
            httpUtil.request("changePasswordForce",jsonStr);
        } catch (IOException e) {
            throw new PasswordChangeException();
        }
    }

    @Override
    public void deleteUser(String userId) throws Exception {
        String jsonStr=JSonString.deleteUser.replace("userId",userId);
        httpUtil.request("deleteUser",jsonStr);
    }

    @Override
    public void ungrantUser(String userId, String projectLabel) throws UserUnGrantException {
        String jsonStr=JSonString.unGrantUser.replace("?",projectLabel).replace("userId",userId);
        try {
            httpUtil.request("unGrantUser",jsonStr);
        } catch (IOException e) {
            throw new UserUnGrantException("取消授权失败，可能的原因：不存在该授权、服务器故障。");
        }
    }

    @Override
    public void deleteRole(String projectLabel) {
        String jsonStr=JSonString.deleteRole.replace("?",projectLabel);
        try {
            httpUtil.request("deleteRole",jsonStr);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteRepository(String projectLabel) {
        String jsonStr=JSonString.deleteRepo.replace("?",projectLabel);

        try {
            httpUtil.request("deleteRepo",jsonStr);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

	@Override
	public void activeUser(String userId) throws IOException {
		// TODO Auto-generated method stub
		String jsonStr=JSonString.activeUser.replace("userId", userId);
		httpUtil.request("activeUser", jsonStr);
	}

	@Override
	public void disableUser(String userId) throws IOException {
		// TODO Auto-generated method stub
		String jsonStr=JSonString.disableUser.replace("userId", userId);
		System.out.println("jsonStr="+jsonStr);
		httpUtil.request("disableUser", jsonStr);
		
	}
}
